package com.linzp.bishe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.linzp.bishe.common.Constants;
import com.linzp.bishe.entity.*;
import com.linzp.bishe.excel.*;
import com.linzp.bishe.model.SimpleAccountUser;
import com.linzp.bishe.service.*;
import com.linzp.bishe.utils.TableName;
import com.linzp.bishe.utils.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName SaveExcelData
 * @Description 保留excel数据的实现类
 * @Author LinZePeng
 * @Date 2022/1/15 10:46
 * @Version 1.0
 */
@Service
public class SaveExcelDataImpl implements SaveExcelData {

	@Autowired
	private AccountsService accountsService;

	@Autowired
	private UsersService usersService;

	@Autowired
	private ParticipantsService participantsService;

	@Autowired
	private AchievementTransformationService achievementTransformationService;

	@Autowired
	private DisciplineCompetitionService disciplineCompetitionService;

	@Autowired
	private HprojectService hprojectService;

	@Autowired
	private PaperService paperService;

	@Autowired
	private PatentService patentService;

	@Autowired
	private PublicationsService publicationsService;

	@Autowired
	private ResearchAchievementsService researchAchievementsService;

	@Autowired
	private SoftwareCopyrightService softwareCopyrightService;

	@Autowired
	private TeachingRewardService teachingRewardService;

	@Autowired
	private ZprojectService zprojectService;

	@Autowired
	private MeetingService meetingService;
	@Override
	public void saveAccountUserExcel(List<AccountsExcel> cachedDataList) {
		List<Users> user = new ArrayList<>();
		List<Accounts> account = new ArrayList<>();

		for (AccountsExcel accountsExcel : cachedDataList) {
			QueryWrapper<Accounts> wrapper = new QueryWrapper<>();
			wrapper.eq("accounts", accountsExcel.getAccount());
			Accounts one = accountsService.getOne(wrapper);
			if (one == null) {
				user.add(accountsExcel.getUsers());
				account.add(accountsExcel.getAccounts());
			}
		}

		accountsService.saveBatch(account);
		for (int i = 0; i < account.size(); i++) {
			user.get(i).setAccountId(account.get(i).getId());
		}
		usersService.saveBatch(user);
	}

	@Override
	public void saveAchievementTransformationExcel(List<AchievementTransformationExcel> cachedDataList) {
		List<AchievementTransformation> list = new ArrayList<>();
		List<Participants> participants = new ArrayList<>();
		SimpleAccountUser accountUser = accountsService.getSimpleAccountUser(Integer.parseInt(ThreadLocalUtils.localVar.get()));
		for (AchievementTransformationExcel excel : cachedDataList) {
			if (!achievementTransformationService.cheakRepeat(excel.getContractName())) {
				list.add(excel.getAchievement()
						.setUserId(accountUser.getUsersId())
						.setInputPeople(accountUser.getParticipantName())
						.setStatus(Constants.NOTSUBMIT));
				participants.add(accountUser.getParticipants().setResearchType(TableName.ACHIEVEMENT));
			}
		}
		if (list.size()>0) {
			achievementTransformationService.saveBatch(list);
			for (int i = 0; i < list.size(); i++) {
				participants.get(i).setResearchFk(list.get(i).getId());
			}
			participantsService.saveBatch(participants);
		}
	}

	@Override
	public void saveDisciplineCompetitionExcel(List<DisciplineCompetitionExcel> cachedDataList) {
		List<DisciplineCompetition> list = new ArrayList<>();
		List<Participants> participants = new ArrayList<>();
		SimpleAccountUser accountUser = accountsService.getSimpleAccountUser(Integer.parseInt(ThreadLocalUtils.localVar.get()));
		for (DisciplineCompetitionExcel excel : cachedDataList) {
			if (!disciplineCompetitionService.cheakRepeat(excel.getWorkName())) {
				list.add(excel.getDiscribedCompetition()
						.setUserId(accountUser.getUsersId())
						.setInputPeople(accountUser.getParticipantName())
						.setStatus(Constants.NOTSUBMIT));
				participants.add(accountUser.getParticipants().setResearchType(TableName.DISCIPLINE));
			}
		}
		if (list.size()>0) {
			disciplineCompetitionService.saveBatch(list);
			for (int i = 0; i < list.size(); i++) {
				participants.get(i).setResearchFk(list.get(i).getId());
			}
			participantsService.saveBatch(participants);
		}
	}

	@Override
	public void saveHprojectExcel(List<HprojectExcel> cachedDataList) {
		List<Hproject> list = new ArrayList<>();
		List<Participants> participants = new ArrayList<>();
		SimpleAccountUser accountUser = accountsService.getSimpleAccountUser(Integer.parseInt(ThreadLocalUtils.localVar.get()));
		for (HprojectExcel excel : cachedDataList) {
			if (!hprojectService.cheakRepeat(excel.getContractName())) {
				list.add(excel.getHproject()
						.setUserId(accountUser.getUsersId())
						.setInputPeople(accountUser.getParticipantName())
						.setStatus(Constants.NOTSUBMIT));
				participants.add(accountUser.getParticipants().setResearchType(TableName.HPROJECT));
			}
		}
		if (list.size()>0) {
			hprojectService.saveBatch(list);
			for (int i = 0; i < list.size(); i++) {
				participants.get(i).setResearchFk(list.get(i).getId());
			}
			participantsService.saveBatch(participants);
		}
	}

	@Override
	public void savePaperExcel(List<PaperExcel> cachedDataList) {
		List<Paper> list = new ArrayList<>();
		List<Participants> participants = new ArrayList<>();
		SimpleAccountUser accountUser = accountsService.getSimpleAccountUser(Integer.parseInt(ThreadLocalUtils.localVar.get()));
		for (PaperExcel excel : cachedDataList) {
			if (!paperService.cheakRepeat(excel.getPaperName())) {
				list.add(excel.getPaper()
						.setUserId(accountUser.getUsersId())
						.setInputPeople(accountUser.getParticipantName())
						.setStatus(Constants.NOTSUBMIT));
				participants.add(accountUser.getParticipants().setResearchType(TableName.PAPER));
			}
		}
		if (list.size()>0) {
			paperService.saveBatch(list);
			for (int i = 0; i < list.size(); i++) {
				participants.get(i).setResearchFk(list.get(i).getId());
			}
			participantsService.saveBatch(participants);
		}
	}

	@Override
	public void savePatentExcel(List<PatentExcel> cachedDataList) {
		List<Patent> list = new ArrayList<>();
		List<Participants> participants = new ArrayList<>();
		SimpleAccountUser accountUser = accountsService.getSimpleAccountUser(Integer.parseInt(ThreadLocalUtils.localVar.get()));
		for (PatentExcel excel : cachedDataList) {
			if (!patentService.cheakRepeat(excel.getPatentName())) {
				list.add(excel.getPatent()
						.setUserId(accountUser.getUsersId())
						.setInputPeople(accountUser.getParticipantName())
						.setStatus(Constants.NOTSUBMIT));
				participants.add(accountUser.getParticipants().setResearchType(TableName.PATENT));
			}
		}
		if (list.size()>0) {
			patentService.saveBatch(list);
			for (int i = 0; i < list.size(); i++) {
				participants.get(i).setResearchFk(list.get(i).getId());
			}
			participantsService.saveBatch(participants);
		}
	}

	@Override
	public void savePublicationExcel(List<PublicationExcel> cachedDataList) {
		List<Publications> list = new ArrayList<>();
		List<Participants> participants = new ArrayList<>();
		SimpleAccountUser accountUser = accountsService.getSimpleAccountUser(Integer.parseInt(ThreadLocalUtils.localVar.get()));
		for (PublicationExcel excel : cachedDataList) {
			if (!publicationsService.cheakRepeat(excel.getPublicationName())) {
				list.add(excel.getPublication()
						.setUserId(accountUser.getUsersId())
						.setInputPeople(accountUser.getParticipantName())
						.setStatus(Constants.NOTSUBMIT));
				participants.add(accountUser.getParticipants().setResearchType(TableName.PUBLICATIONS));
			}
		}
		if (list.size()>0) {
			publicationsService.saveBatch(list);
			for (int i = 0; i < list.size(); i++) {
				participants.get(i).setResearchFk(list.get(i).getId());
			}
			participantsService.saveBatch(participants);
		}
	}

	@Override
	public void saveResearchAchievementsExcel(List<ResearchAchievementsExcel> cachedDataList) {
		List<ResearchAchievements> list = new ArrayList<>();
		List<Participants> participants = new ArrayList<>();
		SimpleAccountUser accountUser = accountsService.getSimpleAccountUser(Integer.parseInt(ThreadLocalUtils.localVar.get()));
		for (ResearchAchievementsExcel excel : cachedDataList) {
			if (!researchAchievementsService.cheakRepeat(excel.getCertificateNumber())) {
				list.add(excel.getResearchAchievements()
						.setUserId(accountUser.getUsersId())
						.setInputPeople(accountUser.getParticipantName())
						.setStatus(Constants.NOTSUBMIT));
				participants.add(accountUser.getParticipants().setResearchType(TableName.RESEARCH));
			}
		}
		if (list.size()>0) {
			researchAchievementsService.saveBatch(list);
			for (int i = 0; i < list.size(); i++) {
				participants.get(i).setResearchFk(list.get(i).getId());
			}
			participantsService.saveBatch(participants);
		}
	}

	@Override
	public void saveSoftwareCopyrightsExcel(List<SoftwareCopyrightsExcel> cachedDataList) {
		List<SoftwareCopyright> list = new ArrayList<>();
		List<Participants> participants = new ArrayList<>();
		SimpleAccountUser accountUser = accountsService.getSimpleAccountUser(Integer.parseInt(ThreadLocalUtils.localVar.get()));
		for (SoftwareCopyrightsExcel excel : cachedDataList) {
			if (!softwareCopyrightService.cheakRepeat(excel.getCopyrightName())) {
				list.add(excel.getSoftwareCopyright()
						.setCopyrightType("软件制品")
						.setUserId(accountUser.getUsersId())
						.setInputPeople(accountUser.getParticipantName())
						.setStatus(Constants.NOTSUBMIT));
				participants.add(accountUser.getParticipants().setResearchType(TableName.SOFTWARE));
			}
		}
		if (list.size()>0) {
			softwareCopyrightService.saveBatch(list);
			for (int i = 0; i < list.size(); i++) {
				participants.get(i).setResearchFk(list.get(i).getId());
			}
			participantsService.saveBatch(participants);
		}
	}

	@Override
	public void saveTeachingRewardExcel(List<TeachingRewardExcel> cachedDataList) {
		List<TeachingReward> list = new ArrayList<>();
		List<Participants> participants = new ArrayList<>();
		SimpleAccountUser accountUser = accountsService.getSimpleAccountUser(Integer.parseInt(ThreadLocalUtils.localVar.get()));
		for (TeachingRewardExcel excel : cachedDataList) {
			if (!teachingRewardService.cheakRepeat(excel.getCertificateNumber())) {
				list.add(excel.getTeachingReward()
						.setUserId(accountUser.getUsersId())
						.setInputPeople(accountUser.getParticipantName())
						.setStatus(Constants.NOTSUBMIT));
				participants.add(accountUser.getParticipants().setResearchType(TableName.TEACHING));
			}
		}
		if (list.size()>0) {
			teachingRewardService.saveBatch(list);
			for (int i = 0; i < list.size(); i++) {
				participants.get(i).setResearchFk(list.get(i).getId());
			}
			participantsService.saveBatch(participants);
		}
	}

	@Override
	public void saveZprojectExcel(List<ZprojectExcel> cachedDataList) {
		List<Zproject> list = new ArrayList<>();
		List<Participants> participants = new ArrayList<>();
		SimpleAccountUser accountUser = accountsService.getSimpleAccountUser(Integer.parseInt(ThreadLocalUtils.localVar.get()));
		for (ZprojectExcel excel : cachedDataList) {
			if (!zprojectService.cheakRepeat(excel.getProjectName())) {
				list.add(excel.getZProject()
						.setUserId(accountUser.getUsersId())
						.setInputPeople(accountUser.getParticipantName())
						.setStatus(Constants.NOTSUBMIT));
				participants.add(accountUser.getParticipants().setResearchType(TableName.ZPROJECT));
			}
		}
		if (list.size()>0) {
			zprojectService.saveBatch(list);
			for (int i = 0; i < list.size(); i++) {
				participants.get(i).setResearchFk(list.get(i).getId());
			}
			participantsService.saveBatch(participants);
		}
	}

	@Override
	public void saveMeetingExcel(List<MeetingExcel> cachedDataList) {
		List<Meeting> list = new ArrayList<>();
		SimpleAccountUser accountUser = accountsService.getSimpleAccountUser(Integer.parseInt(ThreadLocalUtils.localVar.get()));
		for (MeetingExcel excel : cachedDataList) {
			if (!meetingService.cheakRepeat(excel.getMeetingName(),accountUser.getUsersId())) {
				list.add(excel.getMeeting()
						.setUserId(accountUser.getUsersId())
						.setAttendee(accountUser.getParticipantName())
						.setStatus(Constants.NOTSUBMIT));
			}
		}
		if (list.size()>0) {
			meetingService.saveBatch(list);
		}
	}

}
